//
//  FirstViewController.swift
//  swiftTS
//
//  Created by 航电 on 2020/2/27.
//  Copyright © 2020 航电. All rights reserved.
//

import UIKit

class FirstViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var _mainTableView = UITableView();
    let screenSize = UIScreen.main.bounds.size;
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "导航栏渐变";
        self.view.backgroundColor = .white;
        // Do any additional setup after loading the view.
        
        self.onCreateUIForNavigation();
        self.onCreateUIForView();
    }
    
    //TODO:创建UI
    func onCreateUIForNavigation() {
        let backItem = UIBarButtonItem(title: "返回", style: .plain, target: self, action: #selector(backAction));
        self.navigationItem.leftBarButtonItem = backItem;
        
        self.navigationController?.navigationBar.barTintColor = .yellow;
        
        //设备背景图片barTintColor就无效
//        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default);
    }
    
    func onCreateUIForView() {
        _mainTableView = UITableView(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height), style: .plain);
        _mainTableView.delegate = self ;
        _mainTableView.dataSource = self ;
        self.view.addSubview(_mainTableView);
    }
    
    //TODO:Actions
    //TODO:Private Actions
    @objc func backAction() {
        self.navigationController?.dismiss(animated: true, completion: nil);
    }
    
    //TODO:Public Actions
    
    //TODO:UITableView Delegate
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10;
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellId = "first_picture_cell";
        var cell = tableView.dequeueReusableCell(withIdentifier: cellId);
        if cell == nil {
            cell = PitureCell(style: .default, reuseIdentifier: cellId);
        }
        return cell!;
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 340;
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
    //TODO:UIScrollView Delegate
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print("位移：\(scrollView.contentOffset.y)");
        let offsetY = scrollView.contentOffset.y;
        if offsetY <= -200 {
            self.navigationController?.navigationBar.barTintColor = UIColor(red: 255, green: 0, blue: 0, alpha: 1);
            self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white];
        }
        else if offsetY > -200 && offsetY <= -88 {
            self.title = "导航栏渐变";
            let greenValue = 255-(abs(offsetY)-88)*255/112;
            print("g:\(greenValue)");
            self.navigationController?.navigationBar.barTintColor = UIColor.init(red: 255, green: greenValue, blue: 0, alpha: 1-(abs(offsetY)-88)/112);
            self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.darkGray];
        }
        else if offsetY > -88 && offsetY <= 0 {
            self.navigationController?.navigationBar.alpha = abs(offsetY)/88;
            self.title = "点击导航栏回到顶部";
            self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red];
        }
        else if offsetY > 0 {
            self.navigationController?.navigationBar.alpha = 0;
        }
    }
}
